﻿using JMBox.Utils;
using static JMBox.Config.R;

namespace JMBox.Task
{
    public class RefreshTask
    {
        //创建委托
        private delegate void MyDelegate();
        private static MyDelegate _mydelegateRefreshView;


        /// <summary>
        /// 工作内容
        /// </summary>
        private void InnerRun()
        {
            _mydelegateRefreshView = new MyDelegate(RefleshCountView);
            while (GlobalStatus.AppIsRuning && GlobalStatus.RefreshIsOpen)
            {
                Contact.TopUI.Invoke(_mydelegateRefreshView);
            }
        }

        private void RefleshCountView()
        {
            try
            {
                if (Contact.TopUILabel != null)
                {
                    Contact.TopUILabel.Text = "CLEAR:" + ClearTask.SpaceCount
                        + ", MLFLAG:" + GlobalStatus.OpenAutoMolong
                        + ", FINISH:" + Contact.MainUI.FINISH_COUNT
                        + ", WAIT:" + Contact.MainUI.WAIT_TIME;
                }
            }
            catch (Exception e)
            {

            }

        }

        /// <summary>
        /// 开始执行任务
        /// </summary>
        public void Start()
        {
            if (!MacCheckTool.CanUseBat())
            {
                MessageBox.Show("未认证");
                return;
            }
            if (!GlobalStatus.RefreshIsOpen)
            {
                GlobalStatus.RefreshIsOpen = true;
                Thread t = new Thread(InnerRun);
                t.Start();
            }
        }

        /// <summary>
        /// 结束任务
        /// </summary>
        public void End()
        {
            GlobalStatus.RefreshIsOpen = false;
        }
    }
}